package com.lmt_8;

import com.sun.org.apache.bcel.internal.classfile.LocalVariable;

import javax.print.Doc;
import java.sql.SQLOutput;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;

public class HospitalManager {
    // 系统需要存储全部科室信息
    private ArrayList<Department> allDepartments = new ArrayList<>();
    // 系统需要知道全部的预约详情
    private ArrayList<Appointment> appointments = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);

    public void start(){
        while (true){
            System.out.println("===欢迎进入仁爱医院信息系统===");
            System.out.println("1 科室管理-添加科室");
            System.out.println("2 科室管理-删除科室");
            System.out.println("3 科室管理-修改科室");
            System.out.println("4 医生管理-录入医生");
            System.out.println("5 医生管理-医生坐诊设置（可设置当天和未来六天的坐诊状况）");
            System.out.println("6 医生管理-展示全部医生的坐诊详情");
            System.out.println("7 医生管理-挂号预约");
            System.out.println("搜索某个医生和当前和未来六天内的病人预约情况");
            System.out.println("请输入操作指令：");
            Scanner sc = new Scanner(System.in);
            switch (sc.next()){
                case "1":addDepartment();
                    break;
                case "2":
                    break;
                case "3":
                    break;

                case "4":
                    break;
                case "5":
                    break;
                case "6":
                    break;
                case "7":
                    break;
                case "8":
                    break;
            }
        }
    }
    private void addDepartment(){
        System.out.println("==添加科室==");
        out:
        while (true){
            System.out.println("请输入科室名称：");
            String name = sc.next();
            //判断名称是否存在
            for (int i = 0; i < allDepartments.size(); i++){
                if (allDepartments.get(i).getName().equals(name)){
                    System.out.println("名称重复");
                    continue out;
                }
            }
            //没有科室重复
            Department department = new Department();
            department.setName(name);
            allDepartments.add(department);
            System.out.println("添加成功");
            break ;
        }
    }
    private void addDoctor(){
        Doctor doctor = new Doctor();
        // 选择科室
        while (true) {

            Department department = getDepartmentByUser();
            doctor.setDepartmentName(department.getName());
            //录入医生id
            doctor.setDoctorId(UUID.randomUUID().toString());

            System.out.println("请输入医生姓名：");
            String name = sc.next();
            doctor.setName(name);

            System.out.println("请输入医生性别：");
            String gender = sc.next();
            doctor.setGender(gender);

            System.out.println("请输入医生年龄：");
            int age = sc.nextInt();
            doctor.setAge(age);

            System.out.println("请输入医生特长：");
            String specialty = sc.next();
            doctor.setSpecialty(specialty);

            System.out.println("请输入医生入职日期（格式：yyyy-mm-dd）：");
            String joinDateString = sc.next();
            LocalDate joinDate = LocalDate.parse(joinDateString);
            doctor.setJoinDate(joinDate);

            department.getDoctors().add(doctor);
            System.out.println("录入医生成功");
            break;
        }
    }
    private void setDocotorJob(){
        System.out.println("===设置坐诊时间===");
       Department department = getDepartmentByUser();//选择科室
        //选择医生
        ArrayList<Doctor> doctors = department.getDoctors();
        if (doctors.isEmpty()){
            System.out.println("当前科室没有医生");
            return ;
        }
        while (true){
            System.out.println("当前科室的医生如下：");
            for (int i = 0;i < doctors.size(); i++){
                Doctor doctor = doctors.get(i);
                System.out.println(i+1+" "+ doctor.getName());
            }
            System.out.println("清选择：");
            int commond = sc.nextInt();

            if (commond<1 || commond > doctors.size()){
                System.out.println("输入有误，清重新选择");
                continue;
            }
            // 得到医生
            Doctor doctor = doctors.get(commond - 1);
            //为这个医生设置坐诊
            ArrayList<Schedule> schedules = new ArrayList<>();
            // 更新未来的其他时间
            updateSchedules(schedules);

            // 修改坐诊信息，依次展示这个医生的坐 详情
            for (int i = 0; i < schedules.size(); i++){
                Schedule schedule = schedules.get(i);
                updateDoctorSchedule(schedule);
            }
            break;
        }
    }

    private void updateDoctorSchedule(Schedule schedule) {
    }

    private void updateSchedules(ArrayList<Schedule> schedules) {
        if (schedules.isEmpty()){
            for ( int i = 0; i <= 6; i++){
                Schedule schedule = new Schedule();
                LocalDate now = LocalDate.now();
                schedule.setToday(now.plusDays(i));
                schedules.add(schedule);
            }
            return;
        }
        // 去除过期的时间
        for (int i = 0; i< schedules.size(); i++){
            Schedule schedule = schedules.get(i);
            LocalDate now = LocalDate.now();
            LocalDate current = schedule.getToday();
            if (current.equals(now)){
                break;
            }
            if (current.isBefore(now)){
                schedules.remove(schedule);
                i--;
            }
        }
        // 补全当前和未来6天的时间
        LocalDate last = schedules.get(schedules.size() - 1).getToday();
        int size = schedules.size();
        for ( int i = 0; i< 7 - size; i++){
            Schedule schedule = new Schedule();
            schedule.setToday(last.plusDays(i+1));
            schedules.add(schedule);
        }
    }

    // 选择科室
    private Department getDepartmentByUser(){
        if (allDepartments.isEmpty()){
            return null;
        }
        // 选择科室
        while (true){
            System.out.println("清选择科室：");
            for (int i = 0; i < allDepartments.size(); i++) {
                Department department = allDepartments.get(i);
                System.out.println("第" + i + 1 + "个科室" + department.getName());
            }

            System.out.println("请输入：");
            // 接受命令
            int commond = sc.nextInt();
            if (commond < 1 || commond > allDepartments.size()) {
                System.out.println("输入有误，清重新输入");
                continue ;
            }
            Department department = allDepartments.get(commond - 1);
            return department;
        }
    }
}
